package br.com.ngame.manager;

import java.applet.Applet;
import java.awt.MediaTracker;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Vector;

public class AppletImageManager implements ImageManager
{
	protected HashMap<String,Vector<BufferedImage>> collections;
	protected Applet applet;
    private boolean imagesInResource = true;
	
	public AppletImageManager(Applet applet)
	{
		this.applet = applet;
		collections = new HashMap<String,Vector<BufferedImage>>();
	}
	
	public void loadImageCollections(String[] fileNames)
	{
		for (int i=0; i<fileNames.length; i++)
		{
			loadImageCollection(fileNames[i]);
		}
	}
	
	public void loadImageCollection(String fileName)
	{
		Vector<BufferedImage> vect = new Vector<BufferedImage>();
		doLoadImageSheet(vect, fileName);
		collections.put(fileName, vect);
	}
	
	public void loadImage(String fileName)
	{
		BufferedImage img = doLoadImage(fileName);
		Vector<BufferedImage> vect = new Vector<BufferedImage>();
		vect.add(img);
		collections.put(fileName, vect);
	}
	
	public BufferedImage getImage(String fileName, int imageIndex)
	{
		return collections.get(fileName).get(imageIndex);
	}
	
	public BufferedImage getImage(String fileName)
	{
		return collections.get(fileName).get(0);
	}
	
	public int getImageCount(String fileName)
	{
		return collections.get(fileName).size();
	}
	
	protected void doLoadImageSheet(Vector<BufferedImage> vect, String fileName)
	{
		BufferedImage imageFile = doLoadImage(fileName);
		if (imageFile == null)
		{
			System.out.println("Image not loaded");
		}
		else
		{
			ImageProducer source = imageFile.getSource();
			int imageSize = imageFile.getHeight(null);
	        int nImages = imageFile.getWidth(null) / imageSize;
	        for (int i=0; i<nImages; i++)
			{
	        	ImageFilter cropFilter = new CropImageFilter(i*imageSize, 0, imageSize, imageSize);
	        	ImageProducer producer = new FilteredImageSource(source, cropFilter);
	        	BufferedImage tmp = (BufferedImage)applet.createImage(producer);
	        	vect.add( tmp );
			}
		}
	}

    public BufferedImage doLoadImage(String fileName)
    {
    	String url = applet.getDocumentBase().toString();
    	url = url.substring(0, url.lastIndexOf("/")) + "/" + fileName;
    	BufferedImage image = null;
    	try{
    		image = (BufferedImage)applet.getImage(new URL(url));
    	}catch(MalformedURLException e){}
    	if (image == null)
    		return null;
    	else
    	{
	        MediaTracker tracker = new MediaTracker(applet);
	        tracker.addImage(image, 0);
	        try {
	            tracker.waitForID(0);
	        }
	        catch(InterruptedException e){}
	        return image;
    	}
    }
    
    public boolean isImagesInResource() {
        return imagesInResource;
    }

    public void setImagesInResource(boolean value) {
        imagesInResource = value;
    }
}